<?php


namespace app\unify\traits;


use Kayex\HttpCodes;
use think\Paginator;
use think\Response;
use think\response\Json;

trait Result
{

    public static function list($list = [], $count = 0)
    {
        return self::data([
            'list' => $list,
            'count' => $count
        ]);

    }

    public static function page(Paginator $page)
    {
        return self::list($page->getCollection(), $page->total());
    }

    public static function data($data = [], $msg = '操作成功', $code = HttpCodes::HTTP_OK, $other = [])
    {
        return self::json($code, $data, $msg, $other);
    }

    public static function msg($msg = "操作成功", $code = HttpCodes::HTTP_OK)
    {
        return self::json($code, [], $msg);
    }

    /**
     * @notes
     * @param $code
     * @param $data
     * @param $msg
     * @param $other array
     * @version
     * @author EdwardCho
     * @date 2021/4/15 14:11
     */
    public static function json($code, $data, $msg, $other = [])
    {
        return json(array_merge([
            'code' => $code,
            'data' => $data,
            "msg" => $msg,
        ], $other));
    }


    public static function error($msg, $code = HttpCodes::HTTP_INTERNAL_SERVER_ERROR, $data = [], $other = [])
    {
        return self::json($code, $data, $msg, $other);
    }

    public static function str(string $str)
    {
        return self::data([], $str);
    }

}